// @flow
import React, { Component } from 'react'
import styles from './DictSetting.css'
import { Input, Button, Form, Select } from 'antd'
import DictSettingGrid from './DictSettingGrid'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
const FormItem = Form.Item
const Option = Select.Option

type Props = {
  form: Object,
  history: Object
}
type States = {
  rowData: Object,
  current: number,
  total: number,
}
const data = ['location', 'faultType', 'deviceType', 'dataSource']
class DictSetting extends Component {
  props: Props
  state: States
  addDictSetting: Function
  handleSearch: Function
  getData: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      rowData: {},
      current: 1,
      total: 1
    }
    this.addDictSetting = this.addDictSetting.bind(this)
    this.handleSearch = this.handleSearch.bind(this)
    this.getData = this.getData.bind(this)
  }

  componentDidMount () {
    this.getData(this.state.current, 20)
  }

  handleSearch (e: any) {
    const { current } = this.state
    e.preventDefault()
    this.props.form.validateFields((err, values) => {
      if (!err) {
        const label = values.label || ''
        const type = values.type || ''
        const typeCn = values.typeCn || ''
        const value = values.value || ''
        universalFetch(`${__SERVICE_API__}sys/dicts?pageNum=${current}&pageSize=20&label=${label}&type=${type}&typeCn=${typeCn}&value=${value}`)
          .then(res => res.json())
          .then((json) => {
            if (json.code !== 200000) {
              throw new Error(JSON.stringify({ code: json.code, message: json.message }))
            }
            this.setState({
              total: json.pageInfo.total,
              rowData: json.result
            })
          })
          .catch((error) => {
            handleFetchError(error)
            console.error(error)
          })
      }
    })
  }
  getData (page: number, pageSize: number = 20) {
    universalFetch(`${__SERVICE_API__}sys/dicts?pageNum=${page}&pageSize=${pageSize}`)
      .then(res => res.json())
      .then((json) => {
        if (json.code !== 200000) {
          throw new Error(JSON.stringify({ code: json.code, message: json.message }))
        }
        this.setState({
          current: page,
          total: json.pageInfo.total,
          rowData: json.result
        })
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
      })
  }
  addDictSetting () {
    this.props.history.push('/system/dict-add')
  }
  render () {
    const { rowData, current, total } = this.state
    const { getFieldDecorator } = this.props.form
    return (
      <div className={styles['main']}>
        <div className={styles['action']}>
          <Button onClick={this.addDictSetting} style={{ marginTop: '6px' }}>添加字典</Button>
          <Form layout='inline' className={styles['tool-bar']}>
            <FormItem>
              {getFieldDecorator('label')(
                <Input placeholder='显示名称' />
              )}
            </FormItem>
            <FormItem>
              {getFieldDecorator('value')(
                <Input placeholder='回填内容' />
              )}
            </FormItem>
            <FormItem>
              {getFieldDecorator('type')(
                <Input placeholder='字典类型' />
              )}
            </FormItem>
            <FormItem>
              {
                getFieldDecorator('typeCn')(
                  <Input placeholder='类型描述' />
                )
              }
              {
                // getFieldDecorator('type')(
                //   <Select id='type' placeholder='请选择Type'>
                //     {
                //       data.map((item, index) => {
                //         return <Option value={item} key={index}>{item}</Option>
                //       })
                //     }
                //   </Select>
                // )
              }
            </FormItem>
            <Button icon='search' onClick={this.handleSearch} style={{ marginTop: '2px' }} />
          </Form>
        </div>
        <DictSettingGrid {...{
          rowData,
          current,
          total,
          getData: this.getData,
          history: this.props.history,
          pageSize: 20
        }} />
      </div>
    )
  }
}

export default Form.create()(DictSetting)
